import React, { Component } from 'react'
import { Route, Redirect } from 'react-router-dom'
import { hasToken, removeToken } from '@/utils/auth'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as actions from '@/app/Models/actions'


/**
 * 展示页模板设置
 */
@connect(
    state => state.auth || {},
    dispatch => bindActionCreators(actions, dispatch)
)
class AuthorizedRoute extends Component {

  constructor(props) {
      super(props)
        if (hasToken() && !props.userInfo) {
            removeToken()
        }
  }

  render() {
    // component就是UserLayout组件
    const { userInfo, component: Component, ...rest } = this.props
    
    return (
      <Route {...rest} render={props => {
        return userInfo
          ? <Component {...props} />
          : <Redirect to="/auth/login" />
      }} />
    )
  }
}

export default AuthorizedRoute